package org.ihelpuoo.db;

import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.Map;
import java.util.Properties;

import org.ihelpuoo.lang.ObjectUtils;

public class ConnectionWrapper implements ConnectionProxy
{

	private Connection	conn;
	private Statement	statement;
	private ResultSet	rs;

	public static ConnectionProxy wrap(Connection conn)
	{
		if (conn instanceof ConnectionProxy)
			return (ConnectionProxy)conn;
		return new ConnectionWrapper(conn);
	}

	private ConnectionWrapper(Connection conn)
	{
		this.conn = conn;
	}

	public ResultSetProxy queryForward(String sql) throws SQLException
	{
		return query(sql,ResultSet.TYPE_FORWARD_ONLY);
	}

	public ResultSetProxy query(String sql) throws SQLException
	{
		return query(sql,ResultSet.TYPE_SCROLL_INSENSITIVE);
	}

	public ResultSetProxy query(String sql, int scrollType) throws SQLException
	{
		return query(sql,scrollType,ResultSet.CONCUR_READ_ONLY);
	}

	public ResultSetProxy query(String sql, int scrollType, int cursorType) throws SQLException
	{
		statement = conn.createStatement(scrollType,cursorType);
		rs = statement.executeQuery(sql);
		return new ResultSetProxy(rs,this);
	}

	public boolean execute(String sql) throws SQLException
	{
		statement = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);
		return statement.execute(sql);
	}

	public int executeUpdate(String sql) throws SQLException
	{
		statement = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);
		return statement.executeUpdate(sql);
	}

	public ResultSet modify(String tableName) throws SQLException
	{
		statement = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
				ResultSet.CONCUR_UPDATABLE);
		return statement.executeQuery("SELECT * FROM " + tableName);
	}

	public ResultSetMetaData meta(String tableName) throws SQLException
	{
		statement = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
				ResultSet.CONCUR_UPDATABLE);
		return statement.executeQuery("SELECT * FROM " + tableName).getMetaData();
	}

	public Connection connection()
	{
		return this.conn;
	}

	/*
	 * =======================================================
	 */
	/**
	 * @see java.sql.Connection.getAutoCommit()
	 * 
	 * @return true: auto-commit mode. false:
	 * @throws SQLException if a database access error occurs
	 */
	public boolean getAutoCommit() throws SQLException
	{
		return conn.getAutoCommit();
	}

	public void commit() throws SQLException
	{
		conn.commit();
	}

	public boolean isClosed() throws SQLException
	{
		return conn.isClosed();
	}

	public void close() throws SQLException
	{
		conn.close();
	}

	/**
	 * @see java.sql.Connection.setAutoCommit()
	 * 
	 * @param autoCommit <code>true</code> to enable auto-commit mode; 
	 *         <code>false</code> to disable it
	 * @throws SQLException if a database access error occurs
	 */
	public void setAutoCommit(boolean autoCommit) throws SQLException
	{
		if (conn.getAutoCommit() == autoCommit)
			return;
		conn.setAutoCommit(autoCommit);
	}

	public void rollback() throws SQLException
	{
		conn.rollback();
	}

	public void clearWarnings() throws SQLException
	{
		conn.clearWarnings();
	}

	public Statement createStatement() throws SQLException
	{
		return conn.createStatement();
	}

	public Statement createStatement(int resultSetType, int resultSetConcurrency,
			int resultSetHoldability) throws SQLException
	{
		return conn.createStatement(resultSetType,resultSetConcurrency,resultSetHoldability);
	}

	public Statement createStatement(int resultSetType, int resultSetConcurrency)
			throws SQLException
	{
		return conn.createStatement(resultSetType,resultSetConcurrency);
	}

	public String getCatalog() throws SQLException
	{
		return conn.getCatalog();
	}

	public int getHoldability() throws SQLException
	{
		return conn.getHoldability();
	}

	public DatabaseMetaData getMetaData() throws SQLException
	{
		return conn.getMetaData();
	}

	public int getTransactionIsolation() throws SQLException
	{
		return conn.getTransactionIsolation();
	}

	public Map<String, Class<?>> getTypeMap() throws SQLException
	{
		return conn.getTypeMap();
	}

	public SQLWarning getWarnings() throws SQLException
	{
		return conn.getWarnings();
	}

	public boolean isReadOnly() throws SQLException
	{
		return conn.isReadOnly();
	}

	public String nativeSQL(String sql) throws SQLException
	{
		return conn.nativeSQL(sql);
	}

	public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency,
			int resultSetHoldability) throws SQLException
	{
		return conn.prepareCall(sql,resultSetType,resultSetConcurrency,resultSetHoldability);
	}

	public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency)
			throws SQLException
	{
		return conn.prepareCall(sql,resultSetType,resultSetConcurrency);
	}

	public CallableStatement prepareCall(String sql) throws SQLException
	{
		return conn.prepareCall(sql);
	}

	public PreparedStatement prepareStatement(String sql, int resultSetType,
			int resultSetConcurrency, int resultSetHoldability) throws SQLException
	{
		return conn.prepareStatement(sql,resultSetType,resultSetConcurrency,resultSetHoldability);
	}

	public PreparedStatement prepareStatement(String sql, int resultSetType,
			int resultSetConcurrency) throws SQLException
	{
		return conn.prepareStatement(sql,resultSetType,resultSetConcurrency);
	}

	public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
			throws SQLException
	{
		return conn.prepareStatement(sql,autoGeneratedKeys);
	}

	public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException
	{
		return conn.prepareStatement(sql,columnIndexes);
	}

	public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException
	{
		return conn.prepareStatement(sql,columnNames);
	}

	public PreparedStatement prepareStatement(String sql) throws SQLException
	{
		return conn.prepareStatement(sql);
	}

	public void releaseSavepoint(Savepoint savepoint) throws SQLException
	{
		conn.releaseSavepoint(savepoint);
	}

	public void rollback(Savepoint savepoint) throws SQLException
	{
		conn.rollback(savepoint);
	}

	public void setCatalog(String catalog) throws SQLException
	{
		conn.setCatalog(catalog);
	}

	public void setHoldability(int holdability) throws SQLException
	{
		conn.setHoldability(holdability);
	}

	public void setReadOnly(boolean readOnly) throws SQLException
	{
		conn.setReadOnly(readOnly);
	}

	public Savepoint setSavepoint() throws SQLException
	{
		return conn.setSavepoint();
	}

	public Savepoint setSavepoint(String name) throws SQLException
	{
		return conn.setSavepoint(name);
	}

	public void setTransactionIsolation(int level) throws SQLException
	{
		conn.setTransactionIsolation(level);
	}

	public void setTypeMap(Map<String, Class<?>> map) throws SQLException
	{
		conn.setTypeMap(map);
	}

	@Override
	public Array createArrayOf(String arg0, Object[] arg1) throws SQLException
	{
		return null;
	}

	@Override
	public Blob createBlob() throws SQLException
	{
		return null;
	}

	@Override
	public Clob createClob() throws SQLException
	{
		return null;
	}

	@Override
	public NClob createNClob() throws SQLException
	{
		return null;
	}

	@Override
	public SQLXML createSQLXML() throws SQLException
	{
		return null;
	}

	@Override
	public Struct createStruct(String arg0, Object[] arg1) throws SQLException
	{
		return null;
	}

	@Override
	public Properties getClientInfo() throws SQLException
	{
		return null;
	}

	@Override
	public String getClientInfo(String arg0) throws SQLException
	{
		return null;
	}

	@Override
	public boolean isValid(int arg0) throws SQLException
	{
		return false;
	}

	@Override
	public void setClientInfo(Properties arg0) throws SQLClientInfoException
	{
	}

	@Override
	public void setClientInfo(String arg0, String arg1) throws SQLClientInfoException
	{
	}

	@Override
	public boolean isWrapperFor(Class<?> iface) throws SQLException
	{
		if (ObjectUtils.isChildOf(iface,Connection.class))
			return true;
		return false;
	}

	@Override
	public <T> T unwrap(Class<T> iface) throws SQLException
	{
		if (isWrapperFor(iface))
		{
			return (T)rs;
		}
		return null;
	}

}
